package com.uzai.console.service.uzaichat.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.uzaichat.baseconfig.UzaichatBaseConfigDto;
import com.uzai.console.dto.feign.uzaichat.UpdateUzaichatNotifyDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.feign.UzaichatFeignService;
import com.uzai.console.service.uzaichat.UzaichatBaseConfigService;
import com.uzai.console.vo.uzaichat.baseconfig.UzaichatBaseConfigVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class UzaichatBaseConfigServiceImpl implements UzaichatBaseConfigService {

    Logger logger = LoggerFactory.getLogger(UzaichatBaseConfigServiceImpl.class);

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private UzaichatFeignService uzaichatFeignService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询悠聊基础配置基础设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public UzaichatBaseConfigVo findUzaichatBaseConfig(Long merId){

        UzaichatBaseConfigVo uzaichatBaseConfigVo = new UzaichatBaseConfigVo();

        //查询运营商CMS基础设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.UZAICHAT_BASE_CONFIG.getValue());
        if(merBaseConfig != null ){
            uzaichatBaseConfigVo =  JSONObject.parseObject(merBaseConfig.getVal(), UzaichatBaseConfigVo.class);
        }
        return uzaichatBaseConfigVo;
    }

    /**
     * 修改悠聊基础配置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param uzaichatBaseConfigDto
     * @return null
     */
    @Transactional
    public void updateUzaichatBaseConfig(UzaichatBaseConfigDto uzaichatBaseConfigDto){

//        if(StringUtils.isBlank(uzaichatBaseConfigDto.getHangRepay())){
////            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "挂起回复语不能为空");
////        }

//        if(uzaichatBaseConfigDto.getCallKeywordList() == null || uzaichatBaseConfigDto.getCallKeywordList().size() < 1){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "呼叫客服系统关键词不能为空");
//        }
//
//        if(StringUtils.isBlank(uzaichatBaseConfigDto.getCallReply())){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "呼叫客服系统回复语不能为空");
//        }

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(uzaichatBaseConfigDto.getMerId(), MerConfigKeyEnum.UZAICHAT_BASE_CONFIG.getValue());
        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(uzaichatBaseConfigDto)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(uzaichatBaseConfigDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.UZAICHAT_BASE_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(uzaichatBaseConfigDto)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //更新redis
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.UZAICHAT_BASE_CONFIG.getKey(), uzaichatBaseConfigDto.getMerId()), merBaseConfig.getVal());

        //通知客服系统修改了配置
        UpdateUzaichatNotifyDto updateUzaichatNotifyDto = new UpdateUzaichatNotifyDto();
        updateUzaichatNotifyDto.setMerId(uzaichatBaseConfigDto.getMerId());
        UzaiRespVo uzaiRespVo =  uzaichatFeignService.updateNotify(updateUzaichatNotifyDto);
        if(uzaiRespVo.getCode() != ResponseCode.SUCCESS.intValue()){
            logger.error("----修改悠聊配置通知悠聊系统失败------error={}",uzaiRespVo.getMsg());
        }

    }

}
